"""
公共函数封装
"""
import os
import logging
import yaml
import os


class FileUtil:
    """
       文件处理
    """
    data = None

    @classmethod
    def get_file_dir(cls):
        """
          获取项目路径
          :return:
         """
        return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    @classmethod
    def get_project_config_path(cls):
        """
                 获取项目配置路径
                 :return:
        """
        config_path = ''
        __file_dir = cls.get_file_dir()
        for root, dirs, files in os.walk(__file_dir):
            for file in files:
                if os.path.splitext(file)[0] == 'projectconfig':
                    config_path = os.sep.join([root, file])

        return config_path


def read_data_by_yaml(path, key=None):
    """
           从yaml里获取数据
           :return:
    """
    with open(path, encoding='utf-8') as f:
        content = f.read()
        data = yaml.safe_load(content)
        f.close()
    if key is not None:
        for i in key.split('.'):
            try:
                if i in data:
                    data = data[i]
            except Exception as e:
                print(e)
    return data


def get_data_by_dict(data, key=None):
    """
       从字典里获取数据
       :return:
    """
    if key is not None:
        for i in key.split('.'):
            try:
                if i in data:
                    data = data[i]
            except Exception as e:
                print(e)
    return data


def get_config():
    """
    config对象
    :return: config对象
    """
    path = FileUtil().get_project_config_path()
    config = read_data_by_yaml(path)
    return config


class CommonLog:
    """
    日志功能
    """
    test_logger = None
    test_logger_stream_handler = None
    test_logger_file_handler = None
    test_logger_formatter = None

    def __init__(self, name, log_path):

        log_path = os.path.abspath(log_path)
        if os.path.isfile(log_path):
            pass
        else:
            with open(log_path, 'x') as f:
                f.close()
        self.test_logger = logging.getLogger(name)
        self.test_logger.setLevel(logging.DEBUG)
        self.test_logger_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        self.test_logger_stream_handler = logging.StreamHandler()
        self.test_logger_file_handler = logging.FileHandler(log_path, encoding='utf-8')
        self.test_logger_stream_handler.setLevel(logging.DEBUG)
        self.test_logger_file_handler.setLevel(logging.DEBUG)
        self.test_logger_stream_handler.setFormatter(self.test_logger_formatter)
        self.test_logger_file_handler.setFormatter(self.test_logger_formatter)
        self.test_logger.addHandler(self.test_logger_stream_handler)
        self.test_logger.addHandler(self.test_logger_file_handler)

    def log_debug(self, msg):
        self.test_logger.debug(msg)

    def log_info(self, msg):
        self.test_logger.info(msg)

    def log_warning(self, msg):
        self.test_logger.warning(msg)

    def log_error(self, msg):
        self.test_logger.error(msg)
